package com.jiangdong.craft.service.impl;

import com.github.pagehelper.PageHelper;
import com.jiangdong.craft.service.CraftService;
import com.jiangdong.craft.dto.ResponseDTO;
import com.jiangdong.craft.entity.Craft;
import com.jiangdong.craft.entity.CraftProcedure;
import com.jiangdong.craft.entity.Procedure;
import com.jiangdong.craft.example.CraftProcedureExample;
import com.jiangdong.craft.example.RelationExample;
import com.jiangdong.craft.mapper.CraftMapper;
import com.jiangdong.craft.mapper.CraftProcedureMapper;
import com.jiangdong.craft.mapper.ProcedureMapper;
import com.jiangdong.craft.mapper.RelationMapper;
import com.jiangdong.craft.util.CraftUtil;
import com.jiangdong.craft.util.ResponseFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

@Service
public class CraftServiceImpl implements CraftService {

    @Autowired
    CraftMapper craftMapper;
    @Autowired
    CraftProcedureMapper craftProcedureMapper;
    @Autowired
    ProcedureMapper procedureMapper;
    @Autowired
    RelationMapper relationMapper;
    @Autowired
    CraftUtil craftUtil;

    @Transactional(rollbackFor = RuntimeException.class)
    @Override
    public ResponseDTO add(Craft craft) {
        //1.添加数据至craft表
        craftMapper.insert(craft);
        //2.添加节点和图的对应关系表
        if (!CollectionUtils.isEmpty(craft.getProcedures())){
            List<CraftProcedure> craftProcedureList = new ArrayList<>();
            for (Procedure procedure : craft.getProcedures()){
                CraftProcedure craftProcedure = new CraftProcedure();
                craftProcedure.setCraftId(craft.getId());
                craftProcedure.setProcedureId(procedure.getId());
                craftProcedureList.add(craftProcedure);
            }
            craftProcedureMapper.insertBatch(craftProcedureList);
        }

        ResponseDTO responseDTO = ResponseFactory.getResponse(null,"success");
        return responseDTO;
    }

    @Transactional(rollbackFor = RuntimeException.class)
    @Override
    public ResponseDTO delById(Integer id) {
        craftMapper.deleteByPrimaryKey(id);
        //删除流程图-节点表中数据
        CraftProcedureExample craftProcedureExample = new CraftProcedureExample();
        craftProcedureExample.createCriteria().andCraftIdEqualTo(id);
        craftProcedureMapper.deleteByExample(craftProcedureExample);

        //根据流程图id删除节点对应关系中数据
        RelationExample relationExample = new RelationExample();
        relationExample.createCriteria().andCraftIdEqualTo(id);
        relationMapper.deleteByExample(relationExample);

        ResponseDTO responseDTO = ResponseFactory.getResponse(null,"success");
        responseDTO.setMessage("success");
        return responseDTO;
    }

    @Transactional(rollbackFor = RuntimeException.class)
    @Override
    public ResponseDTO<Craft> queryById(Integer id, Integer isProcedure) {
        Craft craft = craftMapper.selectByPrimaryKey(id);

        if (isProcedure.equals(1)){
            craft.setProcedures(craftUtil.getProceduresByCraftId(id));
        }
        ResponseDTO responseDTO = ResponseFactory.getResponse(null,"success");
        responseDTO.setData(craft);

        return responseDTO;
    }

    @Transactional(rollbackFor = RuntimeException.class)
    @Override
    public ResponseDTO<List<Craft>> comprehensiveQuery(Integer p,Integer pageSize,String keyWord, String createTime) {
        PageHelper.startPage(p,pageSize);
        List<Craft> crafts = craftMapper.screeningQuery(keyWord,createTime);
        for (Craft craft : crafts){
            craft.setProcedures(craftUtil.getProceduresByCraftId(craft.getId()));
        }
        ResponseDTO responseDTO = ResponseFactory.getResponse(crafts,"success");
        return responseDTO;
    }

}
